package controllers;

import com.avaje.ebean.Ebean;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import models.Employe;
import models.EvenementAgenda;
import models.Service;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.index;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


/**
 * Created by richard on 31/03/14.
 */
class mtest {
    int num;
    String name;

}

public class monController  extends Controller {

    public static Result index(String chaine) {
        ObjectNode result = Json.newObject();
        mtest c = new mtest();
        c.name = "test name";
        c.num = 42;

        //String name = json.findPath("name").toString() ;//.getTextValue();
        result.put("status", "KO");
        result.put("message", "Missing parameter [name]");


        // test persist
        Employe e = new Employe();
         e.setName("TOTO");

        result.put("message",chaine);
       // Ebean.save(e);
        return ok(result);
    }
    public static Result listeService() {
        List<Service> services = Service.find.all();
        ObjectNode result = Json.newObject();
       // result.put("nombre",services.size()+"" );
        ArrayNode arr = result.arrayNode();
        Iterator i = services.iterator();
        while (i.hasNext()){
            Service s = (Service) i.next();
            arr.add(s.toJson());
        }
       // result.putArray("services").add(arr);
        result.put("services",arr);

        return ok(result);


    }

    public static final String PATTERN = "yyyy-MM-dd";

    //
    public static Result ServiceReel(int id,String dateDeb,String dateFin){
        ObjectNode result = Json.newObject();
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);

        try {
            Date debut=sdf.parse(dateDeb);
            Date fin=sdf.parse(dateFin);

            result.put("id",id+"");
            result.put("datedebut",sdf.format(debut));
            result.put("datefin", sdf.format(fin));



     /*  List<EvenementAgenda> EVTs = EvenementAgenda.find.where()and(
               com.avaje.ebean.Expr.eq("service_id", id),
               com.avaje.ebean.Expr.lt("date_debut", debut),
               com.avaje.ebean.Expr.gt("date_fin", fin)
       ).findList();*/
            List<EvenementAgenda> EVTs =EvenementAgenda.findForIdInIntervalle(debut,fin,id);
            ArrayNode arr = result.arrayNode();
            Iterator i = EVTs.iterator();
            while (i.hasNext()){
                EvenementAgenda E = (EvenementAgenda) i.next();
                arr.add(E.toJson());
            }
            result.put("horaires",arr);

        } catch (ParseException e) {
            e.printStackTrace();
        }




        return ok(result);
    }


    /*****
     * ****
     *
     *
     */
    //
    public static Result ServiceQte(int id,String dateDeb,String dateFin){
        ObjectNode result = Json.newObject();
        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);

        try {
            Date debut=sdf.parse(dateDeb);
            Date fin=sdf.parse(dateFin);

            result.put("id",id+"");
            result.put("datedebut",sdf.format(debut));
            result.put("datefin", sdf.format(fin));



     /*  List<EvenementAgenda> EVTs = EvenementAgenda.find.where()and(
               com.avaje.ebean.Expr.eq("service_id", id),
               com.avaje.ebean.Expr.lt("date_debut", debut),
               com.avaje.ebean.Expr.gt("date_fin", fin)
       ).findList();*/
            List<EvenementAgenda> EVTs =EvenementAgenda.findForIdInIntervalle(debut,fin,id);
            ArrayNode arr = result.arrayNode();
            Iterator i = EVTs.iterator();

            while (i.hasNext()){
                EvenementAgenda E = (EvenementAgenda) i.next();

                //arr.add(E.toJson());
            }
            result.put("horaires",arr);

        } catch (ParseException e) {
            e.printStackTrace();
        }




        return ok(result);
    }

}